<?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Http_Client_Adapter_Socket');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Json');
Zend_Loader::loadClass('Zend_Gdata_App_CaptchaRequiredException');

session_start();
$client = $_SESSION['client'];

function createCalendar($client,$title = 'My New Calendar',$summary = 'A new calendar', $color = '#2952A3',$timezone='America/Los_Angeles', $hidden='false',$where='Berkeley'){
	
	/*
		Calendar creation is not yet supported directly through the Zend GData 
		Client Library at the moment.  But there is a way to leverage the 
		lower level calls from the Zend library to create calendar with raw 
		XML.
	*/

    	$xml = "<entry xmlns='http://www.w3.org/2005/Atom'
	xmlns:gd='http://schemas.google.com/g/2005'
	xmlns:gCal='http://schemas.google.com/gCal/2005'>
	<title type='text'>[TITLE]</title>
	<summary type='text'>[SUMMARY]</summary>
	<gCal:timezone value='America/Los_Angeles'></gCal:timezone>
	<gCal:hidden value='false'></gCal:hidden>
	<gCal:color value='[COLOR]'></gCal:color>
	<gd:where rel='' label='' valueString='Oakland'></gd:where>
	</entry>";
				  
	$gdataCal = new Zend_Gdata_Calendar($client); 
	$uri = 'http://www.google.com/calendar/feeds/default/owncalendars/full'; 
	$xml = str_replace('[TITLE]', $title, $xml);
	$xml = str_replace('[SUMMARY]', $summary, $xml);
	$xml = str_replace('[COLOR]', $color, $xml);
	$gdataCal->post($xml, $uri);
}

function removeCalendar($client, $cid){

}

function shareCalendar($client, $calendar, $email, $permission){
	$xml = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007'>
	  <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/acl/2007#accessRule'/>";
	$xml .= "<gAcl:scope type='user' value='$email'></gAcl:scope>";
	$xml .= "<gAcl:role
	    value='http://schemas.google.com/gCal/2005#$permission'>
	  </gAcl:role>
	</entry>";
	
	$gdataCal = new Zend_Gdata_Calendar($client);
	$uri = "http://www.google.com/calendar/feeds/$calendar/acl/full";
	try{
		$gdataCal->post($xml, $uri);
	}catch(exception $e){
		
	}
}
?>
